import pathToRegexp from 'path-to-regexp/index';
import {getManage,getMaillist,getYLBM,postManage,putManage,deleteManage,getOrgan} from 'services/app';
export default {
    namespace: 'manage',
    state: {
        record:{},
        pages:1,
        getMaterialup:{},
    },
    subscriptions: {
        setupHistory ({ dispatch, history }) {
            history.listen((location) => {
                const match = pathToRegexp('/home').exec(location.pathname);
            });
        },
        setup ({ dispatch }) {},
    },
    effects: {
        //查看登录状态
        *getManage ({ payload }, { call, put }){
            const { data ,total} = yield call(getManage, payload);
            data && data.map((item,index) => item.index = index + 1 );
            yield put({ type: 'save', payload: {getManageData:data, total:total, pages:payload.pageNumber}});
        },
        //获取部门和联系人
        *getAllbumen ({ payload }, { call, put }){
            const data = yield call(getMaillist, {
                name:"",
                phone :"",
                pageNumber :0,
                pageSize :0,
            });
            //const dataTow = yield call(getYLBM);
            const dataTow = yield call(getOrgan, {type:'2'});
            yield put({ type: 'save', payload: {ZRBM:dataTow.data,FZR:data.data}});
        },
        //添加危险隐患
        *postManage ({ payload }, { call, put }){
            const data = yield call(postManage, payload);
            if(data.code == 0){
                yield put({ type: 'save', payload: {pages:0,view:false}});
            }
        },
        //修改隐患
        *putManage ({ payload }, { call, put }){
            const data = yield call(putManage, payload);
            if(data.code == 0){
                yield put({ type: 'save', payload: {pages:0,view:false}});
            }
        },
        //删除安全隐患
        *deleteManage ({ payload }, { call, put }){
            const data = yield call(deleteManage, payload);
            if(data.code == 0){
                yield put({ type: 'save', payload: {pages:0,view:false}});
            }
        },
    },
    reducers: {
        save (state, { payload }) {
            return { ...state, ...payload };
        },
    },
};
